<?php

namespace app\admin\controller\vppz;


/**
 *
 *
 * @icon fa fa-circle-o
 */
class UseCoupon extends Base
{

    /**
     * Service模型对象
     * @var \app\admin\model\vppz\Qxzl
     */
    protected $model = null;

	protected $searchFields = ['id','name'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\vppz\UseCoupon;
        $this->couponmodel = new \app\admin\model\vppz\Coupon;
        $this->userModel = new \app\admin\model\vppz\User;
        $this->hospitalModel = new \app\admin\model\vppz\Hospital;

		// 给数据附加当前appid，appid由Base负责解析
		$this->model::event('before_insert', function ($row){
			$row->app_id=$this->app_id;
		});
    }

/**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->where($where)->where('status','=',2)
                    ->with(['user','coupon','hospital'])
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $k=>$row) {
                $row->getRelation('user')->visible(['nickname','mobile']);
                $row->getRelation('coupon')->visible(['name']);
                $row->getRelation('hospital')->visible(['name']);
//                $user = $this->userModel->where('id',$row['uid'])->find();
//                $list[$k]['user_text'] = $user ? $user['nickname'] : '未知';
//                $coupon = $this->couponmodel->where('id',$row['cid'])->find();
//                $list[$k]['coupon_text'] = $coupon ? $coupon['name'] : '未知';
//                $hospital = $this->hospitalModel->where('id',$row['hospitalid'])->find();
//                $list[$k]['hospital_name'] = $hospital ? $hospital['name'] : '未知';
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

		//$areas = $this->adminAreas();
		//var_dump($areas);
		//var_dump([5,4]);


        return $this->view->fetch();
    }
protected function filter(&$query)
    {
        // 添加性别筛选条件
        if ($this->request->has('hospital_name')) {
            $hospital_name = $this->request->param('hospital_name');
            if ($hospital_name !== '') {
                $query->where('hospital.name','like', '%'.$hospital_name.'%');
            }
        }
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
